﻿using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.KYJScadaProModbusRTU;
using xiaoxiong.KYJDAL.Models;
using xiaoxiong.KYJDAL.Services;
using xiaoxiong.ScadaProModbusRtu.Common;

namespace xiaoxiong.ScadaProModbusRtu
{
    public partial class FrmAlarmLog : Form
    {
        SysLogService SysLogService = new SysLogService();

        public FrmAlarmLog()
        {
            InitializeComponent();
        }

        private string CurrentTime
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
        }


        /// <summary>
        /// 通用添加系统日志
        /// </summary>
        /// <param name="isError"></param>
        /// <param name="info"></param>
        public async void AddLog(bool isError, string info)
        {
            if(dgv_Log.InvokeRequired)
            {
                dgv_Log.Invoke(new Action<bool, string>(AddLog), isError, info);
            }
            else
            {
                int rowCount = dgv_Log.Rows.Add();
                dgv_Log.Rows[rowCount].Cells[0].Value = CurrentTime;
                dgv_Log.Rows[rowCount].Cells[1].Value = isError ? "错误" : "信息";
                dgv_Log.Rows[rowCount].Cells[2].Value = info;

                //存储到数据库
                await SysLogService.AddSysLog(new SysLog
                {
                    InsertTime = CurrentTime,
                    LogType = "系统日志",
                    Note = info,
                    AlarmType = isError ? "错误" : "信息",
                    Operator = CommonMethods.CommonAdmin.LoginName,
                });
            }
        }


        /// <summary>
        /// 通用添加操作日志
        /// </summary>
        /// <param name="isError"></param>
        /// <param name="info"></param>
        public async void AddOPLog(bool isError, string info)
        {
            if (dgv_OpLog.InvokeRequired)
            {
                dgv_OpLog.Invoke(new Action<bool, string>(AddOPLog), isError, info);
            }
            else
            {
                int rowCount = dgv_OpLog.Rows.Add();
                dgv_OpLog.Rows[rowCount].Cells[0].Value = CurrentTime;
                dgv_OpLog.Rows[rowCount].Cells[1].Value = isError ? "错误" : "信息";
                dgv_OpLog.Rows[rowCount].Cells[2].Value = info;


                //存储到数据库
                await SysLogService.AddSysLog(new SysLog
                {
                    InsertTime = CurrentTime,
                    LogType = "操作日志",
                    Note = info,
                    AlarmType = isError ? "错误" : "信息",
                    Operator = CommonMethods.CommonAdmin.LoginName,
                });
            }
        }


        /// <summary>
        /// 通用添加报警日志
        /// </summary>
        /// <param name="isError"></param>
        /// <param name="info"></param>
        public async void AddAlarmLog(SysLog sysLog)
        {
            if (dgv_Alarm.InvokeRequired)
            {
                dgv_Alarm.Invoke(new Action<SysLog>(AddAlarmLog),sysLog);
            }
            else
            {
                int rowCount = dgv_Alarm.Rows.Add();
                dgv_Alarm.Rows[rowCount].Cells[0].Value = sysLog.InsertTime;
                dgv_Alarm.Rows[rowCount].Cells[1].Value = sysLog.AlarmType;
                dgv_Alarm.Rows[rowCount].Cells[2].Value = sysLog.Note;

                //存储到数据库
                await SysLogService.AddSysLog(sysLog);
            }
        }


        private void dgv_Log_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint((DataGridView)sender,e);
        }
    }
}
